package com.diaohaiwen.basic.file.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.crypto.SecureUtil;
import com.diaohaiwen.basic.file.mapper.FilesMapper;
import com.diaohaiwen.basic.file.obj.model.Files;
import com.diaohaiwen.basic.file.service.FilesService;
import com.diaohaiwen.framework.file.BucketRecord;
import com.diaohaiwen.framework.file.MinioUtil;
import com.diaohaiwen.framework.file.obj.FileInfo;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件配置 服务层实现。
 *
 * @author 刁海文
 * @since 2023-12-16 22:41:40
 */
@Service
public class FilesImpl extends ServiceImpl<FilesMapper, Files> implements FilesService {


    @Override
    @SneakyThrows
    public boolean upload(MultipartFile file) {
        long size = file.getSize();
        String originalFilename = file.getOriginalFilename();
        String contentType = file.getContentType();
        String md5 = SecureUtil.md5(file.getInputStream());
        String ext = FileUtil.extName(originalFilename);
        String folder = FileInfo.folder();
        String filename = md5 + "." + ext;
        String path = folder + "/" + filename;
        FileInfo info = FileInfo.build(BucketRecord.file, contentType, folder, filename, size);
        MinioUtil.upload(file.getInputStream(), info);
        return false;
    }


}
